<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>我发布的作业</title>
    <link rel="stylesheet" href="../../component/pear/css/pear.css"/>
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/5.15.3/css/all.min.css" rel="stylesheet">
</head>

<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
            <legend>我发布的作业</legend>
        </fieldset>
        <br>
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<script type="text/html" id="user-toolbar">
    <button class="layui-btn layui-btn-sm" lay-event="add">
        <i class="layui-icon layui-icon-addition"></i>
        新增作业
    </button>
</script>

<script type="text/html" id="user-bar">
    <div class="layui-btn-group">
        <button title="作业详情" class="layui-btn layui-btn-xs" lay-event="check">
            <i class="layui-icon layui-icon-right"></i>
        </button>
        <button title="查看提交情况" class="layui-btn layui-btn-xs" lay-event="log">
            <i class="fa fa-eye" aria-hidden="true"></i>
        </button>
        <button title="删除" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="remove">
            <i class="layui-icon layui-icon-delete"></i>
        </button>
    </div>
</script>
<script type="text/html" id="log-bar">
    <button title="" class="layui-btn layui-btn layui-btn-xs" lay-event="add">
        <i class="layui-icon layui-icon-addition"></i>
    </button>
</script>
<script type="text/html" id="log-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="allDownload">
        <i class="layui-icon layui-icon-download-circle"></i>
        一键下载班级文件
    </button>
</script>
<script src="../../../static/layui/layui.js"></script>
<script src="../../component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let MODULE_PATH = "operate/";

        let cols = [
            [{
                type: 'numbers'
            },
                {
                    title: '作业编号',
                    field: 'taskId',
                    align: 'center'
                },
                {
                    title: '作业标题',
                    field: 'taskName',
                    align: 'center'
                },
                {
                    title: '目标班级',
                    field: 'classes',
                    align: 'center'
                },
                {
                    title: '课程名称',
                    field: 'courseName',
                    align: 'center'
                },
                {
                    title: '截至日期',
                    field: 'deadTime',
                    align: 'center'
                },
                {
                    title: '发布时间',
                    field: 'releaseTime',
                    align: 'center'
                },
                {
                    title: '状态',
                    field: 'status',
                    align: 'center',
                    templet: function (d) {
                        // console.log(d);
                        if (d.status === "100") {
                            return '<span class="layui-badge layui-bg-green">进行中</span>'
                        } else if (d.status === "110") {
                            return '<span class="layui-badge layui-btn-danger">已过期</span>'
                        }
                    }
                },
                {
                    title: '操作',
                    toolbar: '#user-bar',
                    align: 'center',
                    width: 130
                }
            ]
        ]
        let cols_check = [
            [
                {
                    title: '学号',
                    field: 'userId',
                    align: 'center',
                    sort: true
                },
                {
                    title: '姓名',
                    field: 'realName',
                    align: 'center'
                },
                {
                    title: '课程名称',
                    field: 'courseName',
                    align: 'center'
                },
                {
                    title: '班级',
                    field: 'classes',
                    align: 'center'
                },
                {
                    title: '作业附件',
                    field: 'fileName',
                    align: 'center'
                },
                {
                    title: '提交状态',
                    field: 'status',
                    templet: function (d) {
                        if (d.status === 'true') {
                            return '<span class="layui-badge layui-bg-green">' +
                                '已提交</i></span>'
                        } else {
                            return '<span class="layui-badge layui-bg-red">' +
                                '未提交</i></span>'
                        }
                    },
                    align: 'center'
                },
                {
                    title: '批改状态',
                    field: 'checkStatus',
                    templet: function (d) {
                        if (d.checkStatus === 'true') {
                            return '<span class="layui-badge layui-bg-green">' +
                                '已批改</i></span>'
                        } else {
                            return '<span class="layui-badge layui-bg-red">' +
                                '未批改</i></span>'
                        }
                    },
                    align: 'center'
                },
                {
                    title: '操作',
                    align: 'center',
                    templet: function (d) {
                        if (d.status === 'true') {
                            return '<button title="" class="layui-btn layui-btn layui-btn-xs" ' +
                                'lay-event="check">\n' +
                                '        <i class="layui-icon layui-icon-right"></i>\n' +
                                '    </button>';
                        } else {
                            return '';
                        }
                    },
                    width: 80
                }
            ]
        ]

        let tableIns = table.render({
            elem: '#user-table',
            url: '/Task/teacherGetTask',
            method: "post",
            page: {
                layout: ['count', 'prev', 'page', 'next', 'skip']
                , curr: 1
                , groups: 3
                , last: false
            },
            limit: 10,
            //limits:[5,10,20],
            cols: cols,
            id: 'user-table',
            skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新列表',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }],
            //一定要加这个参数代码，否则会由于layui自身的bug导致无法分页
            parseData: function (res) { //将原始数据解析成 table 组件所规定的数据，res为从url中get到的数据
                let result;
                // console.log(res.data[0].birthday);
                // res.data[0].birthday = dateFormat(res.data[0].birthday, 'Y-M-D')
                if (this.page.curr) {
                    result = res.data.slice(this.limit * (this.page.curr - 1), this.limit * this.page.curr);
                } else {
                    result = res.data.slice(0, this.limit);
                }
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.count, //解析数据长度
                    "data": result //解析数据列表
                };
            }
        });

        table.on('tool(user-table)', function (obj) {
            if (obj.event === 'add') {
                window.add(obj);
            } else if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'log') {
                window.log(obj);
            } else if (obj.event === 'check') {
                window.check(obj);
            }
        });


        table.on('toolbar(user-table)', function (obj) {
            if (obj.event === 'add') {
                window.add(obj);
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'search') {
                //obj.del();
                window.search(obj);
            }
        });

        window.check = function (obj) {
            layer.open({
                type: 2,
                title: obj.data['taskName'] + '详情',
                shade: 0.1,
                maxmin: true,
                resize: true,
                shadeClose: true,
                area: ['80%', '90%'],
                content: MODULE_PATH + 'TeaViewOfHomework.html',
                success: function (layero, index) {
                    let body = layer.getChildFrame('body', index);
                    //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                    let iframeWin = window[layero.find('iframe')[0]['name']]
                    body.find('#courseName').append(obj.data['courseName'])
                    body.find('#classes').append(obj.data['classes'])
                    body.find('#taskName').append(obj.data['taskName'])
                    body.find('#deadTime').append(obj.data['deadTime'])
                    body.find('#homeworkContent').append(obj.data['taskClaim'])
                    body.find('#homeworkFiles').append('<a target="_blank" href="' +
                        obj.data['releaseTaskPath'] +
                        '">' + obj.data['fileName'] + '</a>')
                }
            });
        }

        window.log = function (obj) {
            layer.open({
                type: 2,
                title: obj.data['taskName'] + '完成情况',
                shade: 0.1,
                shadeClose: true,
                area: ['100%', '100%'],
                content: MODULE_PATH + 'checkHomeworkSubmit.html',
                success: function (layero, index) {
                    let body = layer.getChildFrame('body', index);
                    //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                    let iframeWin = window[layero.find('iframe')[0]['name']]
                    body.find('#taskId').val(obj.data['taskId'])
                    body.find('#taskName').val(obj.data['taskName'])
                    body.find('#courseName').val(obj.data['courseName'])
                    body.find('#classes').val(obj.data['classes'])
                    iframeWin.layui.table.render({
                        elem: '#user-table',
                        url: '/Task/getStudentsTaskByTaskId',
                        where: {
                            taskId: obj.data['taskId']
                        },
                        page: {
                            layout: ['count', 'prev', 'page', 'next', 'skip']
                            , curr: 1
                            , groups: 3
                            , last: false
                        },
                        limit: 5,
                        //limits:[5,10,20],
                        cols: cols_check,
                        id: 'user-table',
                        skin: 'line',
                        toolbar: '#log-toolbar',
                        defaultToolbar: [],
                        //一定要加这个参数代码，否则会由于layui自身的bug导致无法分页
                        parseData: function (res) { //将原始数据解析成 table 组件所规定的数据，res为从url中get到的数据
                            let result;
                            // console.log(res.data[0].birthday);
                            // res.data[0].birthday = dateFormat(res.data[0].birthday, 'Y-M-D')
                            if (this.page.curr) {
                                result = res.data.slice(this.limit * (this.page.curr - 1), this.limit * this.page.curr);
                            } else {
                                result = res.data.slice(0, this.limit);
                            }
                            return {
                                "code": res.code, //解析接口状态
                                "msg": res.msg, //解析提示文本
                                "count": res.count, //解析数据长度
                                "data": result //解析数据列表
                            };
                        }
                    });
                }
            });
        };

        window.add = function (obj) {
            layer.open({
                type: 2,
                title: '新建作业',
                shade: 0.1,
                maxmin: true,
                resize: true,
                shadeClose: true,
                area: ['70%', '90%'],
                content: MODULE_PATH + 'createHomework.html'
            });
        }

        window.refresh = function (param) {
            table.reload('user-table', {
                url: "/Task/teacherGetTask",
                method: "post"
            });
        }

        window.remove = function (obj) {
            //console.log(obj.data['courseId']);
            layer.confirm('确定要删除' + obj.data['taskName'], {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/Task/teacherDeleteTask",
                    data: {
                        taskId: obj.data['taskId'],
                        courseName: obj.data['courseName'],
                        classes: obj.data['classes'],
                        taskName: obj.data['taskName'],
                    },
                    dataType: 'json',
                    success: function (result) {
                        layer.close(loading);
                        console.log(result);
                        if (result.success) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                tableIns.reload({
                                    url: "/Task/teacherGetTask",
                                    page: {
                                        layout: ['count', 'prev', 'page', 'next', 'skip']
                                        , curr: $(".layui-laypage-em").next().html()
                                        , groups: 3
                                        , last: false
                                    },
                                    method: "post"
                                });
                                obj.del();
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }
    })
</script>
</body>

</html>